package com.xfxz.merchant.myapplication;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.MenuRes;
import android.support.annotation.StringRes;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.xfxz.merchant.R;


public class BaseActivity extends AppCompatActivity implements View.OnClickListener {

    public Toolbar mToolbar;
    private FrameLayout fralay_base_other;
    private TextView mToolbarTitle;
    private TextView toolbar_left;
    private TextView toolbar_right;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_base);
        MyApplication.getInstance().addActivity(this);
        initActionBar();
    }

    /**
     * 添加布局
     *
     * @param layoutID
     */
    public void BaseSetContentView(@LayoutRes int layoutID) {
        fralay_base_other = (FrameLayout) findViewById(R.id.fralay_base_other);
        //      获得inflater
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//      把继承该BaseAcitivyt的layoutID放进来 显示
        View view = inflater.inflate(layoutID, null);
        fralay_base_other.addView(view);
    }


    /**
     * 初始化toolbar
     */
    public void initActionBar() {
        toolbar_left = (TextView) findViewById(R.id.toolbar_left);
        toolbar_right = (TextView) findViewById(R.id.toolbar_right);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbarTitle = (TextView) findViewById(R.id.toolbar_title);

        toolbar_left.setOnClickListener(this);
        toolbar_right.setOnClickListener(this);
    }

    public Toolbar getToolbar() {
        return mToolbar;
    }

    public TextView getToolbarTitle() {
        return mToolbarTitle;
    }

    /**
     * 设置标题颜色
     *
     * @param color 标题颜色
     */
    public void setBarTitleColor(int color) {
        mToolbarTitle.setTextColor(color);

    }


    /**
     * 设置标题栏文字
     *
     * @param title 文字
     */
    public void setBarTitle(String title) {
        mToolbarTitle.setText(title);
        mToolbar.setTitle("");
    }

    public void setBarTitle(@StringRes int title) {
        mToolbarTitle.setText(title);
        mToolbar.setTitle("");
    }
    /**
     * 设置左边文字
     * 默认不显示
     */
    public void setBarLeftText(String str, int visibility){
        toolbar_left.setVisibility(visibility);
        toolbar_left.setText(str);
    }
    /**
     * 设置右边文字
     * 默认不显示
     */
    public void setBarRightText(String str, int visibility){
        toolbar_right.setVisibility(visibility);
        toolbar_right.setText(str);
    }
    /**
     * 设置背景颜色
     * @param color
     */
    public void setBarBackgroundColor(int color){
        mToolbar.setBackgroundColor(color);
    }
    /*
    * 设置右上角菜单
    * */
    public void setRightMenu(@MenuRes int menu) {
        mToolbar.inflateMenu(menu);
    }

    public void setBarMenuOnClickListener(Toolbar.OnMenuItemClickListener onClickListener) {
        mToolbar.setOnMenuItemClickListener(onClickListener);
    }

    /**
     * 是否显示返回键
     * 默认不显示
     */
    public void isBarBackShow(boolean b) {
        if(b){
            setSupportActionBar(mToolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(b);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.getInstance().exit(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toolbar:
                onBarBackClickListener();
                break;
            case R.id.toolbar_left:
                onBarLeftClickListener();
                break;
            case R.id.toolbar_right:
                onBarRightClickListener();
                break;
        }
    }

    /**
     * 左右按钮的点击事件
     */
    private void onBarLeftClickListener() {
    }
    private void onBarRightClickListener() {
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBarBackClickListener();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 返回键点击事件
     */
    public void onBarBackClickListener() {

    }



}
